import numpy as np
import matplotlib.pyplot as plt

def set_chinese_font():
    """设置中文字体"""
    plt.rcParams['font.sans-serif'] = ['SimHei', 'Microsoft YaHei', 'DejaVu Sans']
    plt.rcParams['axes.unicode_minus'] = False  # 解决负号显示问题

set_chinese_font()

def word_problems():
    # 问题1: 行程问题
    def distance_problem():
        # 两车从A、B两地同时出发，相向而行
        speed_a = 60  # 车A速度 km/h
        speed_b = 80  # 车B速度 km/h
        distance_ab = 300  # A、B两地距离 km
        
        # 计算相遇时间
        meeting_time = distance_ab / (speed_a + speed_b)
        
        # 计算相遇地点
        meeting_point_a = speed_a * meeting_time
        meeting_point_b = speed_b * meeting_time
        
        print(f"行程问题:")
        print(f"  两车在 {meeting_time:.2f} 小时后相遇")
        print(f"  相遇点距离A地 {meeting_point_a:.2f} km")
        print(f"  相遇点距离B地 {meeting_point_b:.2f} km")
        
        # 可视化
        time_points = np.linspace(0, meeting_time * 1.2, 100)
        position_a = speed_a * time_points
        position_b = distance_ab - speed_b * time_points
        
        plt.figure(figsize=(10, 4))
        plt.plot(time_points, position_a, 'b-', label='车A位置')
        plt.plot(time_points, position_b, 'r-', label='车B位置')
        plt.axhline(y=meeting_point_a, color='gray', linestyle='--', alpha=0.7)
        plt.axvline(x=meeting_time, color='gray', linestyle='--', alpha=0.7)
        plt.plot(meeting_time, meeting_point_a, 'go', markersize=8, label='相遇点')
        plt.xlabel('时间 (小时)')
        plt.ylabel('距离A地 (km)')
        plt.title('两车相遇问题')
        plt.legend()
        plt.grid(True)
        plt.show()
    
    # 问题2: 混合问题
    def mixture_problem():
        # 有两种浓度的盐水，需要混合得到特定浓度的盐水
        concentration1 = 0.15  # 15%的盐水
        concentration2 = 0.40  # 40%的盐水
        target_concentration = 0.25  # 目标浓度25%
        total_amount = 100  # 总重量100kg
        
        # 设需要第一种盐水x kg，第二种盐水y kg
        # x + y = 100
        # 0.15x + 0.40y = 0.25 * 100
        
        # 构建方程组
        A = np.array([[1, 1], [0.15, 0.40]])
        B = np.array([total_amount, target_concentration * total_amount])
        
        solution = np.linalg.solve(A, B)
        
        print(f"\n混合问题:")
        print(f"  需要 {solution[0]:.2f} kg 的 {concentration1*100}% 盐水")
        print(f"  需要 {solution[1]:.2f} kg 的 {concentration2*100}% 盐水")
        print(f"  混合后得到 {total_amount} kg 的 {target_concentration*100}% 盐水")
    
    distance_problem()
    mixture_problem()

word_problems()